<?php

namespace addons\cms\controller\api;

use addons\cms\model\Comment;
use addons\cms\model\Page;

/**
 * 我的
 */
class My extends Base
{
    protected $noNeedLogin = ['aboutus', 'agreement'];

    /**
     * 我发表的评论
     */
    public function comment()
    {
        $commentList = Comment::with('archives')
        ->where(['user_id' => $this->auth->id])
        ->order('id desc')
        ->paginate();

        foreach ($commentList as $index => $item) {
            $item->create_date = human_date($item->createtime);
        }

        $this->success('', ['commentList' => $commentList]);
    }

    /**
     * 我的消费订单
     */
    public function order(){

        $orderList = \addons\cms\model\Order::with(['archives'])->where('user_id', $this->auth->id)
        ->where('status', 'paid')
        ->order('id', 'desc')
        ->paginate(10, null);

   /*     echo   \addons\cms\model\Order::with(['archives'])->where('user_id', $this->auth->id)
            ->where('status', 'paid')
            ->order('id', 'desc')
            ->buildSql(true);*/


        foreach($orderList as $item){
            $item->createtime = date('Y-m-d H:i:s',$item->createtime);
        }

        $this->success('', ['orderList' => $orderList]);

    }

    /**
     * 我的消费订单
     */
    public function order2(){

        $archives=new \addons\cms\model\Archives();

        $orderList = $archives
            ->alias("a")
            ->join("wh_cms_addonnews n",'a.id=n.id')
            ->where('winid', $this->auth->id)
            ->order('wintime', 'desc')
            ->paginate(10, null)
            ->each(function(&$item, $key){
               // echo   $item->createtime;
                $item->addtime = date("Y/m/d H:i:s", intval( $item->createtime ) );
            })
        ;

        $this->success('', ['orderList' => $orderList]);

    }

    /**
     * 我的消费订单
     */
    public function qiangdan(){

        $userid= $this->auth->id;
        $arid=input();
        $archives=new \addons\cms\model\Archives();

        $orderList = $archives
            ->alias("a")
            ->join("wh_cms_addonnews n",'a.id=n.id')
            ->where('winid', $this->auth->id)
            ->order('wintime', 'desc')
            ->paginate(10, null)
            ->each(function(&$item, $key){
                // echo   $item->createtime;
                $item->addtime = date("Y/m/d H:i:s", intval( $item->createtime ) );
            })
        ;

        $this->success('', ['orderList' => $orderList]);

    }

    /**
     * 关于我们
     */
    public function aboutus()
    {
        $pageInfo = Page::getByDiyname('aboutus');
        if (!$pageInfo || $pageInfo['status'] != 'normal') {
            $this->error(__('单页未找到'));
        }
        $pageInfo = $pageInfo->toArray();
        unset($pageInfo['status'], $pageInfo['showtpl']);
        $this->success('', ['pageInfo' => $pageInfo]);
    }

    /**
     * 用户协议
     */
    public function agreement()
    {
        $pageInfo = Page::getByDiyname('agreement');
        if (!$pageInfo || $pageInfo['status'] != 'normal') {
            $this->error(__('单页未找到'));
        }
        $pageInfo = $pageInfo->toArray();
        unset($pageInfo['status'], $pageInfo['showtpl']);
        $this->success('', ['pageInfo' => $pageInfo]);
    }
}
